package bao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

public class MainForm {
    private JFrame f;

    public MainForm() {
        ShopDealAgency deal = new ShopDealAgency(new ShopDeal());
        this.f = new JFrame();
        f.setSize(1000, 800);
        f.setLocation(400, 300);
        f.setLayout(new GridLayout(2, 1));
        JPanel p1 = new JPanel(new GridLayout(9, 2));
        JLabel l1 = new JLabel("编号");
        JTextField tf1 = new JTextField(12);
        JLabel l2 = new JLabel("姓名");
        JTextField tf2 = new JTextField(8);
        JLabel l3 = new JLabel("店名");
        JTextField tf3 = new JTextField(4);
        JLabel l4 = new JLabel("经营内容");
        JTextField tf4 = new JTextField(3);
        JLabel l5 = new JLabel("店铺地址");
        JTextField tf5 = new JTextField(20);
        JLabel l6 = new JLabel("签发机关");
        JTextField tf6 = new JTextField(10);
        JLabel l7 = new JLabel("登记日期");
        JTextField tf7 = new JTextField(30);
        JPanel p11 = new JPanel(new FlowLayout());
        JButton b1 = new JButton("新增");
        JButton b2 = new JButton("修改");
        JButton b3 = new JButton("删除");
        JButton b4 = new JButton("查询");
        JButton b5 = new JButton("查询所有");
        JLabel l8 = new JLabel("提示：江湖美食店铺系统，地址，签发机关及日期均不需填写和无法修改");
        p1.add(l1);
        p1.add(tf1);
        p1.add(l2);
        p1.add(tf2);
        p1.add(l3);
        p1.add(tf3);
        p1.add(l4);
        p1.add(tf4);
        p1.add(l5);
        p1.add(tf5);
        p1.add(l6);
        p1.add(tf6);
        p1.add(l7);
        p1.add(tf7);
        p11.add(b1);
        p11.add(b2);
        p11.add(b3);
        p11.add(b4);
        p11.add(b5);
        p1.add(p11);
        p1.add(l8);
        f.add(p1);
        JTextArea ta = new JTextArea();
        JScrollPane sp = new JScrollPane(ta);
        f.add(sp);
        f.setVisible(true);
        Shop model = new Shop("金庸武侠佳肴酒水", "江湖", "2021-5-6");
        p1.setBackground(Color.gray);
        b1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Shop shop = null;
                try {
                    shop = (Shop) model.clone();
                    shop.set(tf1.getText(), tf2.getText(), tf3.getText(), tf4.getText());
                } catch (CloneNotSupportedException cloneNotSupportedException) {
                    cloneNotSupportedException.printStackTrace();
                }
                if (deal.add(shop)==1) {
                    JOptionPane.showMessageDialog(f, "添加成功");
                } else
                    JOptionPane.showMessageDialog(f, "添加失败，编号不允许重复");
            }
        });
        b2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Shop shop = null;
                try {
                    shop = (Shop) model.clone();
                } catch (CloneNotSupportedException cloneNotSupportedException) {
                    cloneNotSupportedException.printStackTrace();
                }
                shop.set(tf1.getText(), tf2.getText(), tf3.getText(), tf4.getText());
                if (deal.update(shop) == 1)
                    JOptionPane.showMessageDialog(f, "修改成功");
                else
                    JOptionPane.showMessageDialog(f, "修改失败，该店铺不存在");
            }
        });
        b3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (deal.delete(tf1.getText()) == 1)
                    JOptionPane.showMessageDialog(f, "删除成功");
                else
                    JOptionPane.showMessageDialog(f, "删除失败，该店铺不存在");
            }
        });
        b4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (deal.find(tf1.getText())== null)
                    JOptionPane.showMessageDialog(f, "查询失败，该店铺不存在");
                else {
                    ta.setText("编号\t姓名\t店名\t主营大餐\t地址\t\t签发机关\t\t登记日期\n");
                    ta.append(deal.find(tf1.getText()));
                }
            }
        });
        b5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                ta.setText("编号\t姓名\t店名\t主营大餐\t地址\t\t签发机关\t\t登记日期\n");
                String s = null;
                try {
                    s = deal.findAll();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                if (s == null)
                    JOptionPane.showMessageDialog(f, "搜索失败，店铺为空");
                else
                    ta.append(s);
            }
        });
    }
}
